package com.jxd.dianping.controller.admin;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jxd.dianping.common.*;
import com.jxd.dianping.model.SellerModel;
import com.jxd.dianping.request.PageQuery;
import com.jxd.dianping.request.SellerReq;
import com.jxd.dianping.service.SellerService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.validation.Valid;
import java.util.List;

@Controller
@Slf4j
public class SellerController {

    @Autowired
    private SellerService sellerService;

    // 获取商户列表
    @RequestMapping("/admin/seller/index")
    @AdminPermission
    public ModelAndView index(PageQuery pageQuery) {
        // 设置从第几页开始一页查多少个
        PageHelper.startPage(pageQuery.getPage(), pageQuery.getSize());
        List<SellerModel> sellerModels = sellerService.selectAll(); // sellerModels.size() == pageQuery.getSize()
        // pages 总页数
        // total 总条数
        PageInfo<SellerModel> sellerModelPageInfo = new PageInfo<>(sellerModels);
         ModelAndView modelAndView = new ModelAndView("/admin/seller/index.html");
        modelAndView.addObject("data", sellerModelPageInfo);
        modelAndView.addObject("CONTROLLER_NAME","seller");
        modelAndView.addObject("ACTION_NAME","index");
        return modelAndView;
    }

    // 跳转到创建页
    @RequestMapping("/admin/seller/createPage")
    @AdminPermission
    public ModelAndView createPage() {
        ModelAndView modelAndView = new ModelAndView("/admin/seller/create.html");
        modelAndView.addObject("CONTROLLER_NAME","seller");
        modelAndView.addObject("ACTION_NAME","create");
        return modelAndView;
    }

    @RequestMapping(value = "/admin/seller/create", method = RequestMethod.POST)
    // 不能加@RequestBody的1原因
    // 加上RequestBody 就必须以Json view的方式去解析 已Json的方式去处理请求 ajax json post进行处理
    // 不加上 是以Form的方式去解析处理 key - value & key - value 的形式
    public String create(@Valid SellerReq createSeller, BindingResult bindingResult) throws BusinessException {
        log.info("进入create controller");
        log.info(createSeller.toString());
        if (bindingResult.hasErrors()) {
            throw new BusinessException(EmBusinessError.PARAMETER_VALIDATION_ERROR, CommonUtil.processErrorString(bindingResult));
        }
        SellerModel sellerModel = new SellerModel();
        sellerModel.setName(createSeller.getName());
        sellerService.create(sellerModel);
        return "redirect:/admin/seller/index.html";
    }

    // 禁用商户
    @RequestMapping("/admin/seller/down")
    @ResponseBody
    @AdminPermission
    public CommonRes down(@RequestParam("id") Integer id) throws BusinessException {
        SellerModel seller = sellerService.changeStatus(id, 1);
        return CommonRes.create(seller);
    }

    // 启用商户
    @RequestMapping("/admin/seller/up")
    @ResponseBody
    @AdminPermission
    public CommonRes up(@RequestParam("id") Integer id) throws BusinessException {
        SellerModel seller = sellerService.changeStatus(id, 0);
        return CommonRes.create(seller);
    }
}
